/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhouwei.mzbanner.sample.slice;

import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;
import com.zhouwei.mzbanner.sample.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * MainAbilitySlice
 */
public class MainAbilitySlice extends AbilitySlice {

    private static  int []RES = new int[]{ResourceTable.Media_image5,ResourceTable.Media_image2,ResourceTable.Media_image3,ResourceTable.Media_image4,ResourceTable.Media_image6,ResourceTable.Media_image7,ResourceTable.Media_image8};
    private static  int []BANNER = new int[]{ResourceTable.Media_banner1,ResourceTable.Media_banner2,ResourceTable.Media_banner3,ResourceTable.Media_banner4,ResourceTable.Media_banner5};
    private MZBannerView mMZBanner;
    private MZBannerView mNormalBanner;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mMZBanner = (MZBannerView)findComponentById(ResourceTable.Id_banner);
        mMZBanner.setBannerPageClickListener((view, position) -> {
        });
        mMZBanner.addPageChangeListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
            }

            @Override
            public void onPageSlideStateChanged(int i) {
            }

            @Override
            public void onPageChosen(int i) {
            }
        });
        List<Integer> list = new ArrayList<>();
        for(int i=0;i<RES.length;i++){
            list.add(RES[i]);
        }

        List<Integer> bannerList = new ArrayList<>();
        for(int i=0;i<BANNER.length;i++){
            bannerList.add(BANNER[i]);
        }
        mMZBanner.setIndicatorVisible(true);
        mMZBanner.setPages(bannerList, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
        mNormalBanner = (MZBannerView)findComponentById(ResourceTable.Id_banner_normal);
        mNormalBanner.setPages(list, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
        mMZBanner.start();
        mNormalBanner.start();
        Button btnNet= (Button) findComponentById(ResourceTable.Id_btn_net);
        btnNet.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withAction("action.netimage.slice")
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
            }
        });
    }



    private static class BannerViewHolder implements MZViewHolder<Integer> {
        private Image mImageView;
        @Override
        public Component createView(Context context) {
            // 返回页面布局文件
            Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_banner_item,null,false);
            mImageView = (Image) view.findComponentById(ResourceTable.Id_banner_image);
            return view;
        }

        @Override
        public void onBind(Context context, int position, Integer data) {
            // 数据绑定
            mImageView.setPixelMap(data);
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);

    }
}
